package com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.one

import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityRecognizancePayOneBinding
import com.haoshuang.zhouzhoubang.model.bean.BzjBean
import com.haoshuang.zhouzhoubang.model.bean.BzjData
import com.haoshuang.zhouzhoubang.ui.activity.BondTdActivity2
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.SfActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.pay.preview.PreviewPayDepositActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.NetworkUtil.url
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import java.text.DecimalFormat

/**
 *  冻结金额进入的缴纳保证金
 */
class RecognizancePayOneActivity : BaseVMActivity<RecognizancePayOneViewModel, ActivityRecognizancePayOneBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<RecognizancePayOneActivity>()
        }
    }

    var list = arrayListOf<BzjData.PayCategory>()


    lateinit var adapter: BondTdAdapter5
    lateinit var adapter2: BsortAdapter
    var list2 = arrayListOf<BzjBean.DepositMenu>()

    // 服务分类ID
    private var service_id = ""

    override fun initView(savedInstanceState: Bundle?) {
        service_id = intent.getString("service_id")
        binding.titleBar.title = "保证金"
        binding.titleBar.leftImageView().click { finish() }
        vm.getData2()
        vm.getData1("cate_id" to service_id)
        binding.apply {
            tvBj.clickWithTrigger {
                if (checkbox.isChecked) {
                    if (list.isEmpty()){
                        toast("没有需要支付的保证金")
                        return@clickWithTrigger
                    }
                    startPage<BondTdActivity2>("team_id" to team_id, "service_id" to service_id)

                } else {
                    toast("请勾选协议")
                }

            }
            tvXy.clickWithTrigger {
                val url = "${URLConstants.baseUrl}/index/index/agreement?id=9"
                startPage<HelpDetailActivity>("title" to "师傅保证金协议", "url" to url)
            }


            ivHead.clickWithTrigger {
                startPage<SfActivity>("url" to url)
            }
            rvList2.layoutManager = GridLayoutManager(mActivity, 5)
            rvList.layoutManager = LinearLayoutManager(mActivity)
            adapter = BondTdAdapter5(list)
            adapter2 = BsortAdapter(list2)
            rvList.adapter = adapter
            rvList2.adapter = adapter2
            tvSh.clickWithTrigger {
                finish()
            }
        }

    }
    var url = ""
    override fun initData() {
    }

    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list2.addAll(it?.deposit_menu!!)
                adapter2.notifyDataSetChanged()
                binding.tvWeb.text = it.deposit_agree
                if (!it.deposit_money_pic.isNullOrBlank()){
                    url = it?.deposit_money_pic+""
                }


            }
        }
        vm.getData1.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    list.clear()
                    var order_amount = 0.0
                    for (b in it.not_pay_category) {
                        order_amount += b.team_money.toDouble()
                    }
                    val df = DecimalFormat("#0.00")
                    val order_amount2 = df.format(order_amount)
                    binding.tvPrice.setText("¥${order_amount2}")
                    list.addAll(it.not_pay_category)
                    adapter.notifyDataSetChanged()
                }

            }
        }
    }

}